﻿/**

 @Name：layuiAdmin 用户管理，增删改查
 @Author：马达
 @Site：http://www.sunplus.wang
 @License: 杭州九宸智能科技有限公司
    
 */

layui.define(['table', 'form', 'laydate', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , upload = layui.upload
        , laydate = layui.laydate
        , form = layui.form;

    var varButtonRight = "";
    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , defaultToolbar: ['filter']
        , method: "post"
        , url: layui.setter.APIUrl + '/shop/themeMgr/listPain'
        , headers: { //通过 request 头传递
            Authorization: "bearer " + layui.data('layuiAdmin').access_token
        }
        , parseData: function (res) { //res 即为原始返回的数据 
            varButtonRight = res.Data.ButtonRight;
            return {
                "ResultCode": res.ResultCode, //解析接口状态
                "ResultInfo": res.ResultInfo, //解析提示文本
                "count": res.Data.TotalRecordCount, //解析数据长度
                "Data": res.Data.Items //解析数据列表 
            };
        }
        , cols: [[
            { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100
            { field: 'Name', title: '主题名称' },
            {
                field: 'Image_Upload', title: '主图', templet: function (d) {
                    var html = "<img src=" + layui.setter.ImgPath + d.Image_Upload + " alt='' />";
                    return html;
                }
            },
            {
                field: 'GoodNames', title: '商品', templet: function (d) {
                    return "<span style='font-weight:bold;'>共<span style='color:red'>" + d.FloorNum + "</span>层<span style='color:red'>" + d.GoodNum + "</span>个商品</span>";
                }
            },
            { field: 'DescStr', title: '主题说明' },
            { field: 'StoreName', title: '店铺' },
            {
                title: '操作', minWidth: 165, fixed: 'right', align: 'center', templet: function (d) {
                    var strRtn = "";
                    if (varButtonRight.indexOf('view') >= 0) {
                        strRtn += "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" lay-event=\"view\">查看</a> ";
                    }
                    if (varButtonRight.indexOf('edit') >= 0) {
                        strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
                    }
                    if (varButtonRight.indexOf('del') >= 0) {
                        strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> ";
                    }
                    return strRtn;
                }
            }
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
        }
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        //debugger;
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除吗？', function (index) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/themeMgr/BatchDelete' //实际使用请改成服务端真实接口
                    , data: { "ids": data.PID }
                    , type: "post"
                    //, success: function (res) {
                    //}
                    , done: function (res) {
                        layer.close(index); //执行关闭 
                        table.reload('LAY-app-content-list');
                        layer.msg('删除成功');
                    }
                });
            });
        }
        else if (obj.event === 'edit') {
            //location.hash = '/shop/shop_theme/addChild/id=' + data.PID;
            admin.popup({
                title: '编辑信息'
                    , area: ['60%', '80%']
                , zIndex: 1001
                , id: 'LAY-popup-content-edit'
                , success: function (layero, index) {
                    view(this.id).render('/shop/shop_theme/add', data).done(function () {
                        form.render(null, 'layuiadmin-app-form-list');

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function (data) {
                            var field = data.field; //获取提交的字段 
                            if (field.Image_Upload == "" || field.Image_Upload == null) {
                                return layer.msg("请上传主图");
                            }
                            field.GoodIds = GetGoods();
                            if (field.GoodIds == "" || field.GoodIds == null) {
                                return false;
                            }
                            //提交 Ajax 成功后，关闭当前弹层并重载表格 
                            admin.req({
                                url: layui.setter.APIUrl + '/shop/themeMgr/update' //实际使用请改成服务端真实接口
                                , data: field
                                , type: "post"
                                , done: function (res) {
                                    layui.table.reload('LAY-app-content-list'); //重载表格
                                    layer.close(index); //执行关闭
                                    layer.msg('修改成功');
                                }
                            });
                        });
                    });
                }
            });
        }
        else if (obj.event === 'view') {
            //viewid = data.PID;
            //admin.popup({
            //    title: '商品列表'
            //    , skin: 'layui-layer-lan'
            //    , area: ['85%', '96%']
            //    , zIndex: 1002
            //    , id: 'LAY-popup-content-add2'
            //    , success: function (layero, index) {
            //        lindex = index;
            //        view(this.id).render('/shop/shop_Goods/SelGoods').done(function () {
            //            //form.render(null, 'layuiadmin-app-form-list');

            //        });
            //    }
            //});
            admin.popup({
                title: '查看'
                , skin: 'layui-layer-lan'
                    , area: ['60%', '80%']
                , zIndex: 1002
                , id: 'LAY-popup-content-view'
                , success: function (layero, index) {
                    lindex = index;
                    view(this.id).render('/shop/shop_theme/View', data).done(function () {
                        form.render(null, 'layuiadmin-app-form-list');

                    });
                }
            });
        }
    });

    exports('shop_theme', {})

});
